package com.lotterymanage.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.lotterymanage.page.Pagination;
import com.lotterymanage.page.PaginationQueryBean;

/**
 * dao父类.
 * @author zhangbin
 * @date Apr 19, 2010
 */
public class AbstractDaoImpl extends SqlMapClientDaoSupport {

    /**
     * 自动装配sqlMapClient.
     * @param sqlMapClient SqlMapClient
     */
    @Autowired
    public void setMySqlMapClient(final SqlMapClient sqlMapClient) {
        super.setSqlMapClient(sqlMapClient);
    }

    /**
     * 分页方法.
     * @param countSqlId String 记录数sql
     * @param searchSqlId String 数据sql
     * @param queryBean PaginationQueryBean 分页组件
     * @return Pagination
     */
    @SuppressWarnings("unchecked")
    protected Pagination queryForPage(final String countSqlId, final String searchSqlId,
            final PaginationQueryBean queryBean) {
        PaginationQueryBean entityBean = queryBean == null ? new PaginationQueryBean() : queryBean;

        // 获取符合条件的记录总条数
        int count = (Integer) this.getSqlMapClientTemplate().queryForObject(countSqlId, entityBean);
        Pagination pagination = new Pagination(count, entityBean.getCurPage(), entityBean.getShownum());
        entityBean.setPagination(pagination);
        if (count == 0) {
            return pagination;
        }
        // 取得当页的数据记录
        List<Object> list = (List<Object>) this.getSqlMapClientTemplate().queryForList(searchSqlId, entityBean);
        pagination.setDataList(list);
        entityBean.setPagination(pagination);
        return pagination;
    }
}
